package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel.SubjectDate;
import com.atguigu.eduservice.entity.subject.OneSubject;
import com.atguigu.eduservice.entity.subject.TwoSubject;
import com.atguigu.eduservice.listener.SubjectExcelListener;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-09-25
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {


//    添加课程分类
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService subjectService) {

        try {
            InputStream in = file.getInputStream();
            EasyExcel.read(in, SubjectDate.class,new SubjectExcelListener(subjectService)).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        LambdaQueryWrapper<EduSubject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EduSubject::getParentId,"0");


        List<EduSubject> list = this.list(queryWrapper);

        List<OneSubject> oneSubjects = list.stream().map((item)->{
            OneSubject oneSubject = new OneSubject();
            LambdaQueryWrapper<EduSubject> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(EduSubject::getParentId,item.getId());
            List<EduSubject> list1 = this.list(wrapper);
            List<TwoSubject> twoSubjects=list1.stream().map((i)->{
                TwoSubject twoSubject = new TwoSubject();
                twoSubject.setId(i.getId());
                twoSubject.setTitle(i.getTitle());
                return twoSubject;
            }).collect(Collectors.toList());

            oneSubject.setId(item.getId());
            oneSubject.setTitle(item.getTitle());
            oneSubject.setChildren(twoSubjects);
            return oneSubject;
        }).collect(Collectors.toList());
        return oneSubjects;
    }
}
